<!--
@name:dialogPointer
@author:Zhenglei
@date:14:41
@description:dialogPointer
@update:2022/8/2514:41
-->
<template>
  <el-dialog :title="title"
             :visible.sync="visible"
             :before-close="modalClose"
             :append-to-body="true"
             :close-on-click-modal="false"
             :close-on-press-escape="false"
             width="60%">
    <el-form :model="subData"
             label-position="right"
             label-width="150px"
             ref="formModelRef"
             :rules='formModelRules'>
      <el-form-item label="类型：" class="is-required">
        <el-select v-model="subData.indicationType"
                   class="w-300"
                   :disabled="isEdit"
                   @change="handleChangeType"
                   placeholder="请选择类型">
          <el-option v-for="item in typeDataSource"
                     :key="item.id"
                     :label="item.name"
                     :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="病种指标：" prop="targetId" v-if="subData.indicationType===1">
        <el-select v-model="subData.deviceId"
                   class="w-300"
                   @change="handleChangeDiseases"
                   placeholder="请选择病种">
          <el-option v-for="item in diseasesDataSource"
                     :key="item.id"
                     :label="item.name"
                     :value="item.id">
          </el-option>
        </el-select>


        <el-select v-model="subData.targetId"
                   class="w-300"
                   filterable
                   placeholder="请选择病种指标">
          <el-option v-for="item in diseasesTargetDataSource"
                     :key="item.id"
                     :label="item.projectName"
                     :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="量表：" prop="scaleId" v-if="subData.indicationType===2">
        <el-select v-model="subData.scaleId"
                   class="w-300"
                   filterable
                   @change="handleChangeScale"
                   placeholder="请选择量表">
          <el-option v-for="item in scaleDataSource"
                     :key="item.id"
                     :label="item.scaleName"
                     :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="检测指标：" prop="referenceId" v-if="subData.indicationType===3">
        <el-select v-model="subData.referenceId"
                   class="w-300"
                   filterable
                   @change="handleChangeReference"
                   placeholder="请选择检测指标">
          <el-option v-for="item in detectDataSource"
                     :key="item.id"
                     :label="item.name"
                     :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="基础信息：" prop="indexItemId" v-if="subData.indicationType===4">
        <el-select v-model="subData.indexItemId"
                   class="w-300"
                   filterable
                   @change="handleChangeBase"
                   placeholder="请选择基础信息">
          <el-option v-for="item in diseaseIndexDataSource"
                     :key="item.id"
                     :label="item.name"
                     :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>


      <el-form-item label="指征名称：" prop="indicationName">
        <el-input v-model="subData.indicationName" class="w-300" placeholder="请输入指征名称" clearable/>
      </el-form-item>

    </el-form>
    <p align="center">
      <el-button @click="modalClose"
                 type="info">取 消
      </el-button>
      <el-button type="primary"
                 v-prevent-re-click
                 @click="handleSave()">保 存
      </el-button>
    </p>

  </el-dialog>
</template>

<script>
export default {
  name: "dialogPointer",
  props: {
    dataObj: {
      type: Object,
      default: {}
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      title: '新 增',
      isEdit: false,
      subData: {
        id: null,
        indicationType: 2,
        referenceId: '',
        targetId: '',
        scaleId: '',
        deviceId: '',
        indexItemId: '',
        indicationName: '',
      },
      typeDataSource: [
        // {
        //   name: '病种指标',
        //   id: 1
        // },
        {
          name: '量表指标',
          id: 2
        },
        {
          name: '检测指标',
          id: 3
        },
        {
          name: '基础数据',
          id: 4
        }
      ],
      diseasesDataSource: [],
      diseaseIndexDataSource: [],
      diseasesTargetDataSource: [],
      scaleDataSource: [],
      detectDataSource: [],

      formModelRules: {
        targetId: [{required: true, message: '请选择病种指标', trigger: 'blur'},],
        scaleId: [{required: true, message: '请选择量表', trigger: 'blur'},],
        referenceId: [{required: true, message: '请选择检测指标', trigger: 'blur'},],
        indexItemId: [{required: true, message: '请选择基础信息', trigger: 'blur'},],
        indicationName: [{required: true, message: '请输入指征名称', trigger: 'blur'},],
      }
    }
  },
  created() {
    if (this.dataObj.id) {
      this.title = '编 辑'
      this.isEdit = true
      this.subData = Object.assign(this.subData, this.dataObj)
      this.interfaceJudgment(this.dataObj.indicationType)
      if (this.dataObj.deviceId) {
        this.getHealthMapTargetList(this.dataObj.deviceId)
      }
    } else {
      this.title = '新 增'
      this.isEdit = false
      this.getScaleAll()
    }

  },
  methods: {
    modalClose() {
      this.$emit('update:visible', false);
    },
    handleChangeType(val) {
      this.subData.targetId = ''
      this.subData.referenceId = ''
      this.subData.scaleId = ''
      this.subData.deviceId = ''
      this.subData.indexItemId = ''
      this.subData.indicationName=''
      this.interfaceJudgment(val)
    },
    handleChangeDiseases(val) {
      this.subData.targetId = ''
      this.getHealthMapTargetList(val)
    },
    //选择量表
    handleChangeScale(val) {
      this.indicationName = ''
      if (val) {
        this.subData.indicationName = this.scaleDataSource.find(item => item.id === val).scaleName
      }
    },
    handleChangeReference(val){
      this.indicationName = ''
      if (val) {
        this.subData.indicationName = this.detectDataSource.find(item => item.id === val).name
      }
    },
    handleChangeBase(val){
      this.indicationName = ''
      if (val) {
        this.subData.indicationName = this.diseaseIndexDataSource.find(item => item.id === val).name
      }
    },
    handleSave() {
      this.$refs.formModelRef.validate(valid => {
        if (valid) {
          this.post(this.api.healthMap.saveIndication, this.subData).then((res) => {
            if (res.data.code === '200') {
              this.$message.success('保存成功')
              this.$parent.getList()
              this.modalClose()
            }
          })
        }
      })
    },
    interfaceJudgment(type) {
      switch (type) {
        case 1:
          this.getAllDiseaseList()
          break
        case 2:
          this.getScaleAll()
          break
        case 3:
          this.getAllFieldCNName()
          break
        case 4:
          this.getHealthMapIndexItemList()
          break
      }
    },
    //查询病种指标
    getHealthMapIndexItemList() {
      this.post(this.api.healthMap.getHealthMapIndexItemList).then((res) => {
        if (res.data.code === '200') {
          this.diseaseIndexDataSource = res.data.object
        }
      })
    },
    //查询量表
    getScaleAll() {
      this.post(this.api.healthMap.getScaleAll).then((res) => {
        if (res.data.code === '200') {
          this.scaleDataSource = res.data.object
        }
      })
    },
    //查询检测项指标
    getAllFieldCNName() {
      this.post(this.api.getAllFieldCNName).then((res) => {
        if (res.data.code === '200') {
          this.detectDataSource = res.data.object
        }
      })
    },
    //查询所有病种
    getAllDiseaseList() {
      this.post(this.api.healthMap.getAllDiseaseList, {
        allShowType: 1
      }).then((res) => {
        if (res.data.code === '200') {
          this.diseasesDataSource = res.data.object

        }
      })
    },
    //病种id查询指标
    getHealthMapTargetList(diseaseId) {
      this.post(this.api.healthMap.getHealthMapTargetList, {
        diseaseId: diseaseId,
        pageSize: 200
      }).then((res) => {
        if (res.data.code === '200') {
          this.diseasesTargetDataSource = res.data.object
        }
      })
    },
  },
}
</script>

<style scoped>

</style>
